'''query.py --- fills placeholders in SQL query'''

import sys
import itertools as it


def query():
    x = [-1]
    already = [True]

    def gen(unused):
        if already[0]:
            x[0] += 1
            already[0] = False
            return x[0]
        else:
            already[0] = True
            return x[0]

    d = {}
    for unused_key,v in it.groupby(sys.argv[2:], gen):
        w = list(v)
        d[w[0]] = w[1]

    return open(sys.argv[1]).read() % d

def test_q(sitename, lowrel=10, lowpop=6):
    return open('keywords_of_sites_assignment.sql').read() % {'site_name': sitename,
                                                              'low_relevance': lowrel,
                                                              'low_popularity': lowpop}


if __name__ == '__main__':
    query()

